#!/usr/bin/env python

import unittest
import time
from httputil import HttpUtil
from vm import VM
from storage import Storage
from snapshot import Snapshot
from volume import Volume
from network import NetWork
import util
import config

class All(VM, Storage, Snapshot, Volume, NetWork):

  def __init__(self):
    HttpUtil.__init__(self)


class TestAll(All, unittest.TestCase):

  def __init__(self, *args, **kwargs):
    unittest.TestCase.__init__(self, *args, **kwargs)
    All.__init__(self)

  def setUp(self):
    pass

  def tearDown(self):
    pass

  def test_all(self):
    # clear cloud stack
    
    # account\securitygroup\zone\pod\cluster\host\network

    ## create account
    account = config.account
    password = config.password
    email = config.email
    firstname = config.firstname
    lastname = config.lastname
    accounttype = 1
    domainid = config.domainid

    (accountid, ) = self.createAccount(account, password, email, firstname, lastname, accounttype, domainid)

    ## create securitygroup and securitygroupingress
    securitygroupname = config.securitygroupname
    (securitygroupid, ) = self.createSecurityGroup(securitygroupname, account, domainid)

    securitygroupname = config.securitygroupname
    securitygroupingressprotocal = config.securitygroupingressprotocal
    securitygroupingressstartport = config.securitygroupingressstartport
    securitygroupingressendport = config.securitygroupingressendport
    securitygroupingresscidrlist = config.securitygroupingresscidrlist
    (securitygroupingressid, ) = self.authorizeSecurityGroupIngress(securitygroupid, securitygroupingressprotocal, securitygroupingressstartport, securitygroupingressendport, securitygroupingresscidrlist)

    ## create zone
    zonenetworktype = config.zonenetworktype
    zonename = config.zonename
    zonedns1 = config.zonedns1
    zoneinternaldns1 = config.zoneinternaldns1

    (zoneid,) = self.createZone(zonenetworktype, zonename, zonedns1, zoneinternaldns1)
    self.assertTrue(zoneid > 0)

    ## create pod
    podname = config.podname
    podnetwork = config.podnetwork
    podstartip = config.podstartip
    podendip = config.podendip
    podgateway = config.podgateway

    (podid,) = self.createPod(zoneid, podname, podnetwork, podstartip, podendip, podgateway)
    self.assertTrue(podid > 0)

    ## add cluster
    clustername = config.clustername
    clustertype = config.clustertype
    clusterhypervisor = config.clusterhypervisor

    (clusterid, ) = self.addCluster(zoneid, podid, clustername, clustertype, clusterhypervisor)
    self.assertTrue(clusterid > 0)

    ## create vlan range
    vlanname = config.vlanname
    vlanforvirtualnetwork = config.vlanforvirtualnetwork
    vlanstartip = config.vlanstartip
    vlanendip = config.vlanendip
    vlannetmask = config.vlannetmask
    vlangateway = config.vlangateway

    (vlanid, ) = self.createVlanIpRange(zoneid, podid, vlanname, vlanforvirtualnetwork, vlanstartip, vlanendip, vlannetmask, vlangateway)
    self.assertTrue(vlanid > 0)

    ## add host
    hostusername = config.hostusername
    hostpassword = config.hostpassword
    hosturl = config.hosturl
    hosthypervisor = config.hosthypervisor

    (hostid, ) = self.addHost(zoneid, podid, clusterid, hostusername, hostpassword, hosturl, hosthypervisor)
    self.assertTrue(hostid > 0)

    ## add secondary storage
    secondarystorageurl1 = config.secondarystorageurl1
    secondarystorage1 = self.addSecondaryStorage(secondarystorageurl1, zoneid)
    self.assertEqual(secondarystorage1["name"], secondarystorageurl1)

    time.sleep(20)

    #secondarystorageurl2 = config.secondarystorageurl2
    #secondarystorage2 = self.addSecondaryStorage(secondarystorageurl2, zoneid)
    #self.assertEqual(secondarystorage2["name"], secondarystorageurl2)

    #time.sleep(20)

    ## create storage pool
    storagepoolname = config.storagepoolname
    storagepoolurl = config.storagepoolurl
     
    storagepool = self.createStoragePool(storagepoolname, storagepoolurl, clusterid, zoneid, podid)
    self.assertEqual(storagepool["name"], storagepoolname)
    self.assertEqual(storagepool["zoneid"], zoneid)

    time.sleep(600)

    ## deploy virtual machine
    vmserviceofferingid = config.vmserviceofferingid
    vmtemplateid = config.vmtemplateid

    virtualmachine = self.deployVirtualMachine(vmserviceofferingid, vmtemplateid, zoneid)
    self.assertEqual(virtualmachine["serviceofferingid"], vmserviceofferingid)
    self.assertEqual(virtualmachine["zoneid"], zoneid)
    self.assertEqual(virtualmachine["templateid"], vmtemplateid)

    ## creeate volume
    volumename = config.volumename
    volumediskofferingid = config.volumediskofferingid

    volume = self.createVolume(volumename, zoneid, volumediskofferingid)
    self.assertEqual(volume["name"], volumename)
    self.assertEqual(volume["diskofferingid"], volumediskofferingid)

    ## attach volume
    volumeid = volume["id"]
    volumevirtualmachineid = virtualmachine["id"]

    attachedvolume = self.attachVolume(volumeid, volumevirtualmachineid)
    self.assertEqual(attachedvolume["id"], volumeid)
    self.assertEqual(attachedvolume["virtualmachineid"], volumevirtualmachineid)

    ## create snapshot
    snapshot = self.createSnapshot(volumeid)
    self.assertEqual(snapshot["volumeid"], volumeid)

def get_suite():
  suite = unittest.TestSuite()
  suite.addTest(TestAll("test_all"))
  return suite

if __name__ == "__main__":
  suite = get_suite()
  runner = unittest.TextTestRunner()
  runner.run(suite)
